var MooTabber = new Class({
     initialize: function(el,options) {
	 	
		this.container = $(el);
		this.tabs = new Array();
		this.tabNavs = new Array();

		this.options = Object.extend({
			classMain:this.classMain || 'tabber',
            classMainLive:this.classMainLive || 'tabberlive',
			classTab:this.classTab || 'tabbertab',
			classTabDefault:this.classDefaultTab || 'tabbertabdefault',
			classNav:this.classNav || 'tabbernav',
			classTabHide:this.classTabHide || 'tabbertabhide',
			classNavActive:this.classNavActive || 'tabberactive',
			titleElements:this.titleElements || "h1,h2,h3,h4,h5,h6",
			removeTitle:this.removeTitle || true
		}, options || {});
		this.initTabs();
     },
	 
	 initTabs: function() {
	 	var defaultTab = 0;
		var i = 0;
	 	this.tabs = $ES("."+this.options.classTab,this.container);
		var ul = new Element("ul").addClass(this.options.classNav);
		this.tabs.each(function(tab) {
			var headingText = tab.getProperty("title");
			if(this.options.removeTitle) { tab.setProperty("title",""); }
			if(!headingText) {
				var headingElement = $ES(this.options.titleElements,tab)[0];
				if(headingElement) { headingText = headingElement.innerHTML; }
			}
			if(!headingText) { headingText = 'tab'; }
			var a = (new Element("a").setProperties({
    			href: 'javascript:void(null);',
    			title: headingText,
				onclick:""
			})).setHTML(headingText);
			a.addEvent("click", this.navClick.bindAsEventListener(this));
			var li = new Element("li");
			a.injectInside(li);
			li.injectInside(ul);
			this.tabNavs.push(li);
			if(tab.hasClass(this.options.classTabDefault)) { defaultTab = i; }
			i++;	
		}.bind(this));
		ul.injectBefore(this.container.getFirst());
		this.container.addClass(this.options.classMainLive);
		this.container.removeClass(this.options.classMain);
		this.tabShow(defaultTab);
	 },
	 
	 
	 tabHideAll: function() {
	 	for (i = 0; i < this.tabs.length; i++) {
			this.tabHide(i);
		}
	 },
	 
	 tabHide: function(tabberIndex) {
	 	if(!this.tabs[tabberIndex]) { return false; }
		this.tabs[tabberIndex].addClass(this.options.classTabHide);
		this.navClearActive(tabberIndex);
	 },
	 
	 
	 tabShow: function(tabberIndex) {
	 	if (!this.tabs[tabberIndex]) { return false; }
		this.tabHideAll();
		this.tabs[tabberIndex].removeClass(this.options.classTabHide);
		this.navSetActive(tabberIndex);
	 },
	 
	 navClick: function(event) {
	 	var event = new Event(event);
		var tgt = event.target;
		while(tgt.getTag() != "a")
			tgt = tgt.getParent();
		tgt.blur();
		var idx = this.tabNavs.indexOf(tgt.getParent());
		this.tabShow(idx);
	 },
	 
	 
	 navSetActive: function(tabberIndex) {
	 	//console.log("navSetActive");
	 	this.tabNavs[tabberIndex].addClass(this.options.classNavActive);
	 },
	 
	 navClearActive: function(tabberIndex) {
	 	//console.log("navClearActive");
	 	if(this.tabNavs[tabberIndex]) { this.tabNavs[tabberIndex].removeClass(this.options.classNavActive); }
	 },
	 
	 getCurrentIndex: function() {
	 	var currentIdx = this.tabNavs.indexOf($ES("li."+this.options.classNavActive)[0]);
		return currentIdx;
	 },
	 
	 getNextIndex: function() {
	 	var idx = this.getCurrentIndex();
		if(idx == this.tabs.length-1) { idx = 0; }
		else { idx++; }
		return idx;
	 },
	 
	 getPreviousIndex: function() {
	 	var idx = this.getCurrentIndex();
		if(idx == 0) { idx = this.tabs.length-1; }
		else { idx--; }
		return idx;
	 },
	 
	 next: function() {
		this.tabShow(this.getNextIndex());
	 },
	 
	 previous: function() {
		this.tabShow(this.getPreviousIndex());
	 },
	 
	 removeTab: function(tabberIndex) {
	 	//if(!this.tabs[tabberIndex]) { return false; }
		var newFocus = this.getPreviousIndex();
		this.tabs[tabberIndex].remove();
		this.tabNavs[tabberIndex].remove();
		this.tabShow(newFocus);	 	
	 }
	 
	 /*addTab: function(tabtitle, tabdiv, at) {
	 	var a = (new Element("a").setProperties({
    			href: 'javascript:void(null);',
    			title: headingText,
				onclick:""
			})).setHTML(tabtitle);
		a.addEvent("click", this.navClick.bindAsEventListener(this));
		var li = new Element('li');
		a.injectInside(li);
		
	 }*/
});